package com.module.content.controller

import android.content.Context
import android.util.AttributeSet
import com.allen.base.utils.ViewUtils
import com.module.content.R
import com.module.content.utils.InfoUtils
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import kotlinx.android.synthetic.main.info_video_control.view.*

/**
 * 糗事播放器控制器
 */
class InfoControlVideo : StandardGSYVideoPlayer {

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    /**
     * 是否隐藏进度值
     */
    private var isHideProgress = false

    override fun getLayoutId(): Int {
        return R.layout.info_video_control
    }

    override fun touchSurfaceMoveFullLogic(
        absDeltaX: Float,
        absDeltaY: Float
    ) {
        super.touchSurfaceMoveFullLogic(absDeltaX, absDeltaY)
        //不给触摸快进，如果需要，屏蔽下方代码即可
        mChangePosition = false

        //不给触摸音量，如果需要，屏蔽下方代码即可
        mChangeVolume = false

        //不给触摸亮度，如果需要，屏蔽下方代码即可
        mBrightness = false
    }

    override fun init(context: Context?) {
        super.init(context)
        setGSYVideoProgressListener { progress, _, currentPosition, duration ->
            if (!isHideProgress) {
                bottom_progressbar?.progress = progress
                ViewUtils.setVisible(current)
                current?.text = InfoUtils.getDuration(duration - currentPosition)
            } else {
                hideProgress()
            }
        }
        againPlayIv.setOnClickListener {
            ViewUtils.setGone(playFinishLayout)
            startPlayLogic()
        }
    }


    /**
     * 设置隐藏进度值
     */
    fun setHideProgress(isHide: Boolean) {
        isHideProgress = isHide
        if (isHideProgress) {
            hideProgress()
            GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL)
        } else {
            GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT)
        }
    }

    //隐藏进度显示
    private fun hideProgress() {
        ViewUtils.setGone(bottom_progressbar)
        ViewUtils.setGone(current)
    }

    override fun onAutoCompletion() {
        super.onAutoCompletion()
        if (!isHideProgress) {
            ViewUtils.setVisible(playFinishLayout)
        }
    }

    override fun touchDoubleUp() {
        //super.touchDoubleUp();
        //不需要双击暂停
    }
}